﻿<?php
include_once("../initial.php");
include_once("../dbdriver/messageWHandle.php");
include_once("../dbdriver/messageRHandle.php");

include_once("userLogic.php");
include_once("adminLogic.php");
include_once("messageLogic.php");


class messageLogic
{
    public function insert_message($array)
    {
        if(is_array($array)==false)
        {
            return false;
        }
        if(count($array)==0)
        {
            return false;
        }
        $whandle = new messageWHandle();
        return $whandle->insert_data($array);
    }
    public function get_message($array='', $orderby='', $limit='')
    {
        $rhandle = new messageRHandle();
        if($array == '')
        {
            return $rhandle->get_all_data($orderby,$limit);
        }
        else
        {
            return $rhandle->get_some_data($array, $orderby, $limit);
        }
    }
    public function get_unread_message_num($uid)
    {
    		$array=array("recieveuid"=>$uid,"read"=>0, "rdelete"=>0);
    		return $this->get_message_num($array);
    }
    public function get_message_num($array='')
    {
        $rhandle = new messageRHandle();
        if(is_array($array)==false)
        {
            $array = array();
        }
        return $rhandle->get_message_num($array);
    }
    public function set_read($messageId)
    {
    	$array = array("read"=>"1");
    	return $this->update_message($array, $messageId);
    }
    public function update_message($array,$id)
    {
        $handle = new messageWHandle();
        return $handle->update_message($array,$id);
    }
    public function delete_message($id)
    {
        $handle = new messageWHandle();
        return $handle->delete_message($id);
    }
    public function delete_send_message($id)
    {
        $array=array();
        $array['sdelete']="1";
        $handle = new messageWHandle();
        return $handle->update_message($array, $id);
    }
    public function delete_recieve_message($id)
    {
        $array=array();
        $array['rdelete']="1";
        $handle = new messageWHandle();
        return $handle->update_message($array, $id);
    }

    public function get_unread_message($uid)
    {
    		$array=array("recieveuid"=>$uid, "read"=>0,"rdelete"=>0);
    		$handle = new messageRHandle();
        return $handle->get_some_data($array, "order by createtime desc","");
    }
    public function get_all_message($orderby="", $limit="")
    {
        $handle = new messageRHandle();
        if($orderby=="")
        {
            $orderby="order by createtime desc";
        }
        return $handle->get_all_data($orderby,$limit);
    }


    public function get_user_send($uid)
    {
        $array = array();
        $array['senduid']=$uid;
        $array['sdelete']="0";
        $handle = new messageRHandle();
        return $handle->get_some_data($array, "order by createtime desc","");
    }

    public function get_user_recieve($uid)
    {
        $array=array();
        $array['revieveuid']=$uid;
        $array['rdelete']="0";
        $handle = new messageRHandle();
        return $handle->get_some_data($array, "order by createtime desc","");
    }
    public function get_user_notread($uid)
    {
        $array=array();
        $array['revieveuid']=$uid;
        $array['read']="0";
        $array['rdelete']="0";
        $handle = new messageRHandle();
        return $handle->get_some_data($array, "order by createtime desc","");
    }



}
?>
